# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=opencv
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1.0
pkgrel=1
pkgdesc="Open Source Computer Vision Library (mingw-w64)"
arch=('any')
url="http://opencv.org/"
depends=(#"${MINGW_PACKAGE_PREFIX}-gst-plugins-base0.10"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         #"${MINGW_PACKAGE_PREFIX}-qt5"
         #"${MINGW_PACKAGE_PREFIX}-gtkglext"
         #"${MINGW_PACKAGE_PREFIX}-gtk2"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             #"${MINGW_PACKAGE_PREFIX}-qt5"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
             "${MINGW_PACKAGE_PREFIX}-python2-numpy"
             "${MINGW_PACKAGE_PREFIX}-python3-numpy"
             "${MINGW_PACKAGE_PREFIX}-vtk"
            )
optdepends=("${MINGW_PACKAGE_PREFIX}-eigen3"
            "${MINGW_PACKAGE_PREFIX}-ffmpeg: support to read and write video files"
            "${MINGW_PACKAGE_PREFIX}-python2-numpy: Python 2.x interface"
            "${MINGW_PACKAGE_PREFIX}-python3-numpy: Python 3.x interface"
            "${MINGW_PACKAGE_PREFIX}-vtk: opencv_viz module")
source=("${_realname}-${pkgver}.tar.gz"::https://github.com/Itseez/opencv/archive/${pkgver}.tar.gz
        'mingw-w64-cmake.patch'
        'solve_deg3-underflow.patch'
        'issue-4107.patch'
        'generate-proper-pkg-config-file.patch'
        'opencv-support-python-3.5.patch')
sha256sums=('f00b3c4f42acda07d89031a2ebb5ebe390764a133502c03a511f67b78bbd4fbf'
            '22c8e8892e6e1e88ad130518567b7e30fb5fabfc117ae257f055cde000ba8100'
            'fd4e095c3c879413184fc6b91a7b0a77dbb128612341a8be2c99d804a203e362'
            '52ebc8875b9ef3ea70897f34509228daeff73d0cab0aa9eb8b931be6a7d32d7f'
            '47447c78acc810cd0604b641644f1c546c29e925b7b9671a4fa18468ff3ad330'
            'e50f69c6c1fbce255b3e7ef4b489bf1a01ba28cf828a6adb80c73a7f9c08a666')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/mingw-w64-cmake.patch"
  patch -Np1 -i "${srcdir}/solve_deg3-underflow.patch"
  patch -Np1 -i "${srcdir}/issue-4107.patch"
  patch -Np1 -i "${srcdir}/generate-proper-pkg-config-file.patch"
  patch -Np1 -i "${srcdir}/opencv-support-python-3.5.patch"
}

build() {
  # SSE only available from Pentium 3 onwards (i686 is way older)
  [[ "$CARCH" = 'i686' ]] && {
    _cmakeopts=('-D ENABLE_SSE=OFF'
      '-DENABLE_SSE2=OFF'
      '-DENABLE_SSE3=OFF')
    CXXFLAGS+=" -DEIGEN_DONT_VECTORIZE"
    _ffmpeg_plugin='opencv_ffmpeg.dll'
  }

  # all x64 CPUs support SSE2 but not SSE3
  [[ "${CARCH}" = "x86_64" ]] && {
    _cmakeopts+=('-DENABLE_SSE3=OFF')
    _ffmpeg_plugin='opencv_ffmpeg_64.dll'
  }

  # pushd ${srcdir}/${_realname}-${pkgver}/3rdparty/ffmpeg > /dev/null
  # rm -f ./*.dll
  # gcc -Wall -shared -o ${_ffmpeg_plugin} ${CXXFLAGS} -x c++ \
    # -I../include -I../../modules/highgui/src -I../../modules/videoio/src ffopencv.c \
    # -lavformat -lavcodec -lavdevice -lswscale -lavutil -lws2_32
  # popd > /dev/null

  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}


  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPKG_CONFIG_WITHOUT_PREFIX=ON \
    -DWITH_OPENCL=ON \
    -DWITH_OPENGL=ON \
    -DWITH_TBB=ON \
    -DWITH_XINE=OFF \
    -DBUILD_WITH_DEBUG_INFO=OFF \
    -DBUILD_DOCS=OFF \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DBUILD_EXAMPLES=ON \
    -DINSTALL_C_EXAMPLES=ON \
    -DINSTALL_PYTHON_EXAMPLES=ON \
    -DWITH_GTK=OFF \
    -DWITH_QT=OFF \
    -DWITH_VTK=ON \
    -DWITH_FFMPEG=ON \
    -DWITH_GSTREAMER=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SKIP_RPATH=ON \
    -DENABLE_PRECOMPILED_HEADERS=OFF \
    ${_cmakeopts[@]} \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR=${pkgdir} -j1 install
}
